package com.order.controller;

import com.order.Vo.MyOrderVo;
import com.order.entity.POrder;
import com.order.entity.User;
import com.order.service.UserService;
import com.order.service.imp.OrderServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderServiceImp orderService;
    @Autowired
    UserService userService;
    //查询我的订单
    @RequestMapping("/selectOrder")
    @ResponseBody
    public List<MyOrderVo> selectOrder(Long buyerId, Integer state){
        List<MyOrderVo> pOrders = orderService.selectOrder(buyerId,state);
        return pOrders;
    }

    //通过当前用户获取订单
    @RequestMapping("/selectMyOrder")
    @ResponseBody
    public List<MyOrderVo> selectMyOrder(String name,Integer state){
        //获取当前用户信息
        //User userToken = UserUtils.getCurrUserFromCookie("token", User.class, request);
        //通过名字获取用户数据
        User user = userService.selectUser(name);
        //获取当前用户的订单
        List<MyOrderVo> pOrders = orderService.selectOrder(user.getId(),state);
        return pOrders;
    }

    //支付我的订单
    @RequestMapping("/pay")
    @ResponseBody
    public Integer pay(String name,Long productId, Integer state){
        //通过名字获取用户数据
        User user = userService.selectUser(name);
        //通过商品获得订单
        POrder pOrder = orderService.myOrder(user.getId(),productId);
        System.out.println(user.getId()+"_____________________________"+pOrder.getId());
        //通过订单Id修改订单状态
        Integer integer = orderService.updateOrder(user.getId(),pOrder.getId(), state);
        return integer;
    }
}
